﻿using ExamPlat.Repositories;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using ExamPlat.Models;
using ExamPlat.DAL;
using ExamPlat.ViewModels;

namespace ExamPlat.Controllers
{
    public class ExamQuesController : Controller
    {
        private static IExamQuesRepository examQuesRepository = new ExamQuesRepository();
        // GET: ExamQues
        public ActionResult Index(string searchTitle)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    ViewBag.Msg = "你没有权限查看该页面";
                    return View("Error");
                }
                ViewBag.RoleID = curUser["RoleID"];
                IList<ExamQues> examQuesList = examQuesRepository.GetExamQuesByCondition(searchTitle);
                ViewBag.SearchTitle = searchTitle;
                return View(examQuesList);
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }


        public ActionResult Create(string searchTitle)
        {
            if (Request.Cookies["CurUser"] == null)
            {
                return RedirectToAction("Login", "Login");
            }
            HttpCookie curUser = Request.Cookies["CurUser"];
            if (curUser["RoleID"] != "1")
            {
                ViewBag.Msg = "你没有权限查看该页面";
                return View("Error");
            }
            ViewBag.RoleID = curUser["RoleID"];
            ViewBag.SearchTitle = searchTitle;
            return View();
        }

        [HttpPost]
        public ActionResult Create(ExamQues model)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                ViewBag.RoleID = curUser["RoleID"];

                model.ExamQuesStatus = false;
                model.ExamQuesDesc = "";
                int result = examQuesRepository.AddExamQues(model);
                if (result == 1)
                {
                    return RedirectToAction("Index", "ExamQues", new { searchTitle = Request.Form["searchTitle"] });
                }
                else if (result == -1)
                {
                    ViewBag.Msg = "考题标题已经存在";
                    ViewBag.SearchTitle = Request.Form["searchTitle"];
                    return View(model);
                }
                else
                {
                    ViewBag.Msg = "增加失败";
                    ViewBag.SearchTitle = Request.Form["searchTitle"];
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }


        public ActionResult Edit(int id, string searchTitle)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    ViewBag.Msg = "你没有权限查看该页面";
                    return View("Error");
                }
                ViewBag.RoleID = curUser["RoleID"];
                ViewBag.SearchTitle = searchTitle;
                ExamQues examQues = examQuesRepository.GetExamQuesByID(id);
                return View(examQues);
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }

        [HttpPost]
        public ActionResult Edit(ExamQues model)
        {
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                ViewBag.RoleID = curUser["RoleID"];
                int result = examQuesRepository.UpdateExamQues(model);
                if (result == 1)
                {
                    return RedirectToAction("Index", "ExamQues", new { searchTitle = Request.Form["searchTitle"] });
                }
                else if (result == -1)
                {
                    ViewBag.Msg = "考题标题已经存在";
                    ViewBag.SearchTitle = Request.Form["searchTitle"];
                    return View(model);
                }
                else
                {
                    ViewBag.Msg = "修改失败";
                    ViewBag.SearchTitle = Request.Form["searchTitle"];
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                ViewBag.Msg = ex.Message;
                return View("Error");
            }
        }


        [HttpPost]
        public ActionResult Delete(int id)
        {
            JsonResult result = new JsonResult();
            try
            {
                if (Request.Cookies["CurUser"] == null)
                {
                    return RedirectToAction("Login", "Login");
                }
                HttpCookie curUser = Request.Cookies["CurUser"];
                if (curUser["RoleID"] != "1")
                {
                    result.Data = new { msg = "你没有权限操作" };
                }
                else
                {
                    int tmpResult = examQuesRepository.DelExamQues(id);
                    if (tmpResult == 1)
                    {
                        result.Data = new { msg = "删除成功" };
                    }
                    else
                    {
                        result.Data = new { msg = "删除失败" };
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                Log4Helper.Info(ex.Message, ex);
                result.Data = new { msg = "删除异常" };
                return result;
            }
        }

        public ActionResult Preview(int id)
        {
            try
            {
                ViewExamQues model = examQuesRepository.PreviewExamQues(id);
                return View(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}